//
//  editSelfInfoViewController.m
//  one_mile_01
//
//  Created by 王进帅 on 15/10/30.
//  Copyright © 2015年 王雅蓉. All rights reserved.
//

#import "editSelfInfoViewController.h"

@interface editSelfInfoViewController ()

@property (nonatomic, strong) UITextField *editTextF;
@property (nonatomic, strong) UITextView *editTextV;

@end

@implementation editSelfInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BACKGROUND_ASSISTANT_COLOR;
    self.navigationController.navigationBar.hidden = YES;
    
    UIView *navigationV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, NAVIGATIONHEIGHT)];
    navigationV.backgroundColor = BACKGROUND_MAIN_COLOR;
    [self.view addSubview:navigationV];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:NAVIGATION_TITLE_FRAME];
    titleLabel.text = self.titleString;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = NAVIGATION_TITLE_FONT;
    titleLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:titleLabel];

    UIButton *editSelfInfoCancel = [UIButton buttonWithType:UIButtonTypeCustom];
    editSelfInfoCancel.frame = CGRectMake(5, titleLabel.frame.origin.y, 80, 30);
    [editSelfInfoCancel setTitle:@"取消" forState:UIControlStateNormal];
    [editSelfInfoCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [editSelfInfoCancel addTarget:self action:@selector(editSelfInfoCancelAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:editSelfInfoCancel];
    
    UIButton *editSelfInfoSafeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    editSelfInfoSafeButton.titleLabel.font = editSelfInfoCancel.titleLabel.font;
    editSelfInfoSafeButton.frame = CGRectMake(WIDTH - 85, titleLabel.frame.origin.y, 80, 30);
    [editSelfInfoSafeButton setTitle:@"保存" forState:UIControlStateNormal];
    [editSelfInfoSafeButton setTitleColor:FONT_ASSISTANT_COLOR forState:UIControlStateNormal];
    [self.view addSubview:editSelfInfoSafeButton];
    
    [editSelfInfoSafeButton addTarget:self action:@selector(editSelfInfoSaveAction:) forControlEvents:UIControlEventTouchUpInside];
    
    if ([titleLabel.text isEqualToString:@"个人简介"]) {
        
        UIView *editTextVt = [[UIView alloc] initWithFrame:CGRectMake(0, NAVIGATIONHEIGHT + 15 * (HEIGHT / 667.0), WIDTH, 200)];
        editTextVt.backgroundColor = [UIColor whiteColor];
        
        self.editTextV = [[UITextView alloc]init];
        self.editTextV.font = [UIFont systemFontOfSize:15];
        self.editTextV.backgroundColor = [UIColor whiteColor];
        self.editTextV.text = self.titleInfoStr;

        if (iPhone6) {
            
            self.editTextV.frame = CGRectMake(15, 0, WIDTH - 30, 200);
        }else{
            
            self.editTextV.frame = CGRectMake(0, 0,WIDTH - 15 * (HEIGHT / 667.0), 200);
        }
        [self.view addSubview:editTextVt];
        [editTextVt addSubview:_editTextV];
    } else {
        UIView *editTextVt = [[UIView alloc] initWithFrame:CGRectMake(0, NAVIGATIONHEIGHT + 15 * (HEIGHT / 667.0), WIDTH, 45 * HEIGHT / 667.0)];
        editTextVt.backgroundColor = [UIColor whiteColor];
        self.editTextF = [[UITextField alloc]init];
        self.editTextF.clearButtonMode = UITextFieldViewModeAlways;
        self.editTextF.text = self.titleInfoStr;
        self.editTextF.backgroundColor = [UIColor whiteColor];
        if (iPhone6) {
            
            _editTextF.frame = CGRectMake(15, 0, WIDTH - 30, 45);
        }else{
            
            _editTextF.frame = CGRectMake(0, 0, WIDTH, 45 * (HEIGHT / 667.0));
        }
        [self.view addSubview:editTextVt];
        [editTextVt addSubview:_editTextF];
    }
    // Do any additional setup after loading the view.
    
    UITapGestureRecognizer *endEditGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEditAction:)];
    [self.view addGestureRecognizer:endEditGR];
}

-(void)endEditAction:(UITapGestureRecognizer *)tap
{
    [self.view endEditing:YES];
}

+ (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{
    float fPadding = 16.0; // 8.0px x 2
    CGSize constraint = CGSizeMake(textView.contentSize.width - fPadding, CGFLOAT_MAX);
    
    CGSize size = [strText sizeWithFont: textView.font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    
    float fHeight = size.height + 16.0;
    
    return fHeight;
}

-(void)editSelfInfoSaveAction:(UIButton *)button
{
    if ((_editTextF != nil && _editTextF.text.length != 0) || (_editTextV != nil && _editTextV.text.length != 0)) {
        if (_editTextF != nil) {
            [self.myDelegate changeInfoForself:_editTextF.text withIndex:_indexForEditself];
        } else {
            [self.myDelegate changeInfoForself:_editTextV.text withIndex:_indexForEditself];
        }
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2] animated:YES];
    } else {
    
        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertV show];
    }
}

-(void)editSelfInfoCancelAction{

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
